﻿using System;
using System.IO;

/* This class takes a text file, parses it and constructs a map from it.
 * An example text file would be as follows:
 * 
 * 
 * #mapheight=12
 * #mapwidth=50
 * #maptexture=3
 * #walltexture=2
 * 
 * 11111111111111111111111111111111111111111111111111
 * 10000000000000000000000000000000000000000000000001
 * 12222222222000000000000000000000000000000000000001
 * 11111111112000000000000000000000000000000000000001
 * 10000000012000000000000000000000000000000000000001
 * 10000000012000000000000000000000000000000000000001
 * 10000000012000000000000000000000000000000000000001
 * 12211111112000000000000000000000000000000000000001
 * 12222222222000000000000000000000000000000000000001
 * 10000000012000000000000000000000000000000000000001
 * 10000000012000000000000000000000000000000000000001
 * 11111111111111111111111111111111111111111111111111
 * 
 * In the above example number 1 represents a wall, number 2
 * represents a "heavier path" (used for avoiding sprite overlapping with
 * zombie-wall collision detection and A* zombie pathing)
 * and number 0 represents ground
 * 
 * The map maker can choose from a selection of wall and map textures.
 * Map width and size may vary.
 */

namespace ZoambieGaem
{

    class MapParser
    {

        private int[][] map;
        //private File mapfile;
        private int mapheight;
        private int mapwidth;
        private String bufferString = "";
        private Char[] charbuffer;
        private int maptexture;
        private int walltexture;
        private TextReader tr;
        private Maps maps;

        public MapParser(Master m, Maps map, String filepath)
        {
            maps = map;
            int settingsOk = 0;
            int settingValue = 0;
            String path = "C:/Placeholderi/mapfile";
            FileStream fs = new FileStream(path, FileMode.Open);
            tr = new StreamReader(fs);
            for (int i = 1; i <= 4; i++)
            {
                bufferString = tr.ReadLine();
                if (bufferString.StartsWith("#mapheight"))
                {
                    settingValue = parseInt(bufferString);
                    Maps.height = settingValue;
                    settingsOk++;
                }
                else if (bufferString.StartsWith("#mapwidth"))
                {
                    settingValue = parseInt(bufferString);
                    Maps.width = settingValue;
                    settingsOk++;
                }
                else if (bufferString.StartsWith("#maptexture"))
                {
                    settingValue = parseInt(bufferString);
                    maptexture = settingValue;
                    settingsOk++;
                }
                else if (bufferString.StartsWith("#walltexture"))
                {
                    settingValue = parseInt(bufferString);
                    maptexture = settingValue;
                    settingsOk++;
                }
            }

            if (settingsOk == 4)
                parseMap();
            else
                //use default map


                tr.Close();
        }

        private void parseMap()
        {
            int j = 0;
            char c = (char)tr.Peek();
            while (j < mapheight) //while (c != null && j < mapheight)          anto jotai errorii
            {
                bufferString = tr.ReadLine();
                charbuffer = bufferString.ToCharArray();
                for (int i = 0; i < mapwidth; i++)
                {
                    if (isValid(charbuffer[i]))
                    {
                        Maps.map[j][i] = (int)charbuffer[i];
                    }
                    else Maps.map[j][i] = 1;
                }

                j++;
                tr.Peek();
            }
        }

        /*
         * Parses the integer setting values for mapsize and textures
         * from the beginning of the mapfile
         */

        private int parseInt(String s)
        {
            string charBuffer = "";
            char[] characters = new char[s.Length];
            characters = s.ToCharArray();
            for (int i = 0; i < s.Length; i++)
            {
                if (Char.IsNumber(characters[i]))
                    charBuffer = charBuffer + characters[i];
            }

            int returnValue = Convert.ToInt16(charBuffer);

            return returnValue;
        }

        /*
         * 
         * 
         */

        private bool isValid(Char c)
        {
            bool valid = false;

            if (c.Equals(1))
                valid = true;
            else if (c.Equals(2))
                valid = true;
            else if (c.Equals(0))
                valid = true;

            return valid;
        }
    }
}
